﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.FlowStep
{
    public partial class Published : PageBase
    {
        public string ID
        {
            get
            {
                return GetQueryIntValue("id");
            }
        }
        public string PublishedContent { get; set; }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            SetPageShowContent();
        }
        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(this.CurrentStandID))
            {
                var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);

                //PartyJoinProcess ps = DB.PartyJoinProcesss.Find(this.CurrentStepID);
                if (oModel != null)
                {
                    if (!string.IsNullOrEmpty(oModel.Remark))
                        this.PublishedContent = oModel.Remark;
                }
            }

            //页面只读显示
            //页面只读显示
            if (this.ReadOnly.Trim() == "1")
            {
                this.btnSubmit.Visible = false;
                this.btnSaveClose.Visible = false;
                //if (IsJoinPartyAdmin)
                //{
                //    this.btnSaveClose.Visible = true;
                //}
            }
        }



        private void SaveData(bool isClose, bool AdminSave = false)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过

            bool bResult = false;
            var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (AdminSave)
            {
                oModel.Remark = this.Request.Form["editor"];
                DB.SaveChanges();
            }
            else
            {
                DataSet ds = ModuleUtil.BranchLoginStantID(this.ApplyBranchID, oModel.CurrentStepID,this.CurrentStandID);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    var oModelEx = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                    oModelEx.Remark = this.Request.Form["editor"];
                    oModelEx.Status = (int)Enum_FlowStanceStatus.通过;
                    oModelEx.HandleUserID = this.CurrentUser.LoginID;
                    oModelEx.HandleUserName = this.CurrentUser.UserName;
                    oModelEx.HandleTime = DateTime.Now;

                    bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);
                    //发布转为党员账号  预备党员
                    if (this.CurrentStepID == ProcesUtil.JoinStep28)
                    {
                        ModuleUtil.UpdateUserMassage(oModel.AppllicationID, "0", "null");
                    }
                }
            }
            if (bResult)
            {
                if (AdminSave)
                    Alert.Show("保存成功", String.Empty, ActiveWindow.GetHideReference());
                else
                    Alert.Show("提交成功", String.Empty, ActiveWindow.GetHideReference());
            }
            else
            {
                if (!string.IsNullOrEmpty(Message))
                    Alert.Show(Message, String.Empty, ActiveWindow.GetHideReference());
            }
        }

        //设置页面显示内容
        protected void SetPageShowContent()
        {
            string MassageName = string.Empty;

            var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (oModel != null)
            {
                switch (oModel.CurrentStepID)
                {
                    case ProcesUtil.JoinStep4:
                    case ProcesUtil.JoinStep24:
                    case ProcesUtil.JoinStep36://公示
                        MassageName = "公示内容"; break;
                    //入党公示
                    //MassageName = "入党公示"; break;
                    case ProcesUtil.JoinStep27://发放通知
                        MassageName = "预备党员<br/>批复通知书"; break;
                    case ProcesUtil.JoinStep28: //公布1
                        MassageName = "公布"; break;

                    //MassageName = "公示"; break;
                    case ProcesUtil.JoinStep39: //公布2
                        MassageName = "公布"; break;
                    default: MassageName = "备注"; break;
                }
            }
            this.lbPublished.Label = MassageName;
        }

        #endregion

        #region
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string outMassage = "";
            if (string.IsNullOrEmpty(ZHDJ.Core.Util.FilterSpecialCharacters.Filter(this.Request.Form["editor"])))
            {
                outMassage = lbPublished.Label.Replace("<br/>", "").Replace("：", "") + "不能为空!";
                Alert.Show(outMassage, String.Empty);
                return;
            }
            this.SaveData(true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(true, true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}